//
//  GetDeviceDetailViewController.m
//  Rail
//
//  Created by hychen on 2018/7/22.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "GetDeviceDetailViewController.h"
#import "ButtonTableViewCell.h"
#import "WarnDetailTableViewCell.h"

@interface GetDeviceDetailViewController ()

@end

@implementation GetDeviceDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem addSimpleTitleView:@"领用详情"];
    self.tableView.mj_header = nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dict = [self.dataArray objectAtIndexSafe:indexPath.row];
    if (![dict isKindOfClass:[NSDictionary class]]) {
        return [UITableViewCell new];
    }
    BMWeakSelf
    if ([[dict objectForKeySafe:@"type"] isEqualToString:@"desc"]) {
        WarnDetailTableViewCell *cell = [WarnDetailTableViewCell cellWithTableView:tableView class:[WarnDetailTableViewCell class]];
        cell.contentLabel.textAlignment = NSTextAlignmentLeft;
        cell.titleLabel.text = [dict objectForKeySafe:@"title"];
        cell.contentLabel.text = [dict objectForKeySafe:@"content"];
        cell.rightConstraints.constant = 15.f;
        return cell;
    }
    else if ([[dict objectForKeySafe:@"type"] isEqualToString:@"button"]) {
        ButtonTableViewCell *cell = [ButtonTableViewCell cellWithTableView:tableView class:[ButtonTableViewCell class]];
        cell.buttonTitle = [dict objectForKeySafe:@"title"];
        [cell setBlockClickEventButton:^{
            [weakSelf requestData];
        }];
        return cell;
    }
    else {
        return [UITableViewCell new];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dict = [self.dataArray objectAtIndexSafe:indexPath.row];
    if (![dict isKindOfClass:[NSDictionary class]]) {
        return 0;
    }
    if ([[dict objectForKey:@"type"] isEqualToString:@"desc"]) {
        return 45;
    }
    else if ([[dict objectForKey:@"type"] isEqualToString:@"button"]) {
        return 200;
    }
    return 0;
}

- (void)requestData {
    if (![self.model isKindOfClass:[DeviceInfoModel class]]) {
        Show_Msg_Error(@"数据错误")
        return;
    }
    Show_NoMsg
    if ([self.model.typecode isEqualToString:@"VOIP-A"]) {
        [self requestTbk];
    }
    else if ([self.model.typecode isEqualToString:@"GPS"]){
        [self requestLocate];
    }
    else {
        Show_Msg_Error(@"类型出错")
    }
}

- (void)requestLocate {
    BMWeakSelf
    [HttpRequest returnLocateDeviceWithDeviceId:self.model.deviceid?:@"" success:^(id object) {
        Show_SV_Success_Msg(@"归还设备成功");
        if (weakSelf.BlockReturnSuccess) {
            weakSelf.BlockReturnSuccess();
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [weakSelf.navigationController popViewControllerAnimated:YES];
            });
        }
    } failure:^(NSString *code, NSString *msg) {
        Show_Msg_Error(@"归还设备失败");
    }];
}

- (void)requestTbk {
    BMWeakSelf
    NSString *account = [GlobalManage sharedManage].userInfoModel.account?:@"";
    [HttpRequest returnTbkDeviceWithDeviceId:self.model.deviceid?:@"" account:account success:^(id object) {
        Show_SV_Success_Msg(@"归还设备成功");
        if (weakSelf.BlockReturnSuccess) {
            weakSelf.BlockReturnSuccess();
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [weakSelf.navigationController popViewControllerAnimated:YES];
            });
        }
    } failure:^(NSString *code, NSString *msg) {
        Show_Msg_Error(@"归还设备失败");
    }];
}

- (NSMutableArray *)dataArray {
    NSString *regTime = self.model.reg_time.length > 19 ? [self.model.reg_time substringToIndex:19] : self.model.reg_time;
    if (self.model.user_type == 0) {
        return @[
                 @{@"type":@"desc",@"title":@"设备编号:",@"content":self.model.deviceid?:@"无",},
                 @{@"type":@"desc",@"title":@"设备名称:",@"content":self.model.device_name?:@"无",},
                 @{@"type":@"desc",@"title":@"所在工区:",@"content":self.model.area_name?:@"无",},
                 @{@"type":@"desc",@"title":@"用户姓名:",@"content":self.model.username?:@"无",},
                 @{@"type":@"desc",@"title":@"手机号码:",@"content":self.model.mobile?:@"无",},
                 @{@"type":@"desc",@"title":@"领用时间:",@"content":regTime?:@"",},
                 @{@"type":@"button",@"title":@"立即归还",@"content":@"",},
                 ].mutableCopy;
    }

    else {
        return @[
                 @{@"type":@"desc",@"title":@"设备编号:",@"content":self.model.deviceid?:@"无",},
                 @{@"type":@"desc",@"title":@"设备名称:",@"content":self.model.device_name?:@"无",},
                 @{@"type":@"desc",@"title":@"所在工区:",@"content":self.model.area_name?:@"无",},
                 @{@"type":@"desc",@"title":@"用户姓名:",@"content":self.model.username?:@"无",},
                 @{@"type":@"desc",@"title":@"手机号码:",@"content":self.model.mobile?:@"无",},
                 @{@"type":@"desc",@"title":@"单位名称:",@"content":self.model.company?:@"无",},
                 @{@"type":@"desc",@"title":@"领用时间:",@"content":regTime?:@"",},
                 @{@"type":@"button",@"title":@"立即归还",@"content":@"",},
                 ].mutableCopy;
    }
}

- (void)setModel:(DeviceInfoModel *)model {
    _model = model;
    [self.tableView reloadData];
}
@end
